<?php
include_once '../classes/design.php';
include_once '../classes/clienteServicos.php';
include_once '../../classes/erro.php';
include_once '../../classes/cliente.php';
include_once '../../classes/manipuladorXML.php';

// Salvar, se tiver pedido pra salvar
if (isset($_REQUEST["salvar"])) {
	$params["id"] = $_REQUEST["id"];
	$params["nome"] = $_REQUEST["nome"];
	$params["email"] = $_REQUEST["email"];
	$params["cpf"] = $_REQUEST["cpf"];
	$params["telefone1"] = $_REQUEST["telefone1"];
	$params["telefone2"] = $_REQUEST["telefone2"];
	$params["rua"] = $_REQUEST["rua"];
	$params["numero"] = $_REQUEST["numero"];
	$params["complemento"] = $_REQUEST["complemento"];
	$params["bairro"] = $_REQUEST["bairro"];
	$params["cidade"] = $_REQUEST["cidade"];
	$params["estado"] = $_REQUEST["estado"];
	$params["cep"] = $_REQUEST["cep"];
	$xml = ClienteServicos::requisitar("clientes/salvar.php", $params);
	$cliente = ManipuladorXML::desserializar($xml);
	if (gettype($cliente) == "object" && get_class($cliente) == "Erro") {
		header("Location: index.php?erro=" . $cliente->get_msg());
		exit;
	}
	else {
		header("Location: index.php?sucesso=Cliente " . $cliente->get_nome() . " salvo!");
		exit;
	}
}

// Recuperar o objeto
if (isset($_REQUEST["id"])) {
	$xml = ClienteServicos::requisitar("clientes/get.php", array("id" => $_REQUEST["id"]));
	$cliente = ManipuladorXML::desserializar($xml);
	if (gettype($cliente) == "object" && get_class($cliente) == "Erro") {
		header("Location: index.php?erro=" . $cliente->get_msg());
		exit;
	}
}

// Criar novo objeto, caso não tenha sido instanciado
if (!isset($cliente)) {
	$cliente = new Cliente();
}

$design = new Design("..");
$design->imprimir_topo();

echo "
	<h1>Clientes</h1>
";

if ($cliente->get_id() == -1) {
	echo "<h2>Novo cliente</h2>";
}
else {
	echo "<h2>Alterar cliente</h2>";
}

echo "
	<form method=\"post\" action=\"" . $_SERVER["PHP_SELF"] . "\">
		<input type=\"hidden\" name=\"id\" value=\"" . $cliente->get_id() . "\" />
		<input type=\"hidden\" name=\"salvar\" value=\"1\" />
		<table border=\"0\" align=\"center\">
			<tr>
				<td align=\"right\">Nome *</td>
				<td>
					<input type=\"text\" name=\"nome\" value=\"" . $cliente->get_nome() . "\" size=\"80\"/>
				</td>
			</tr>
			<tr>
				<td align=\"right\">Email *</td>
				<td>
					<input type=\"text\" name=\"email\" value=\"" . $cliente->get_email() . "\"  size=\"80\"/>
				</td>
			</tr>
			<tr>
				<td align=\"right\">CPF *</td>
				<td>
					<input type=\"text\" name=\"cpf\" value=\"" . $cliente->get_cpf() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">Telefone 1 *</td>
				<td>
					<input type=\"text\" name=\"telefone1\" value=\"" . $cliente->get_telefone1() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">Telefone 2</td>
				<td>
					<input type=\"text\" name=\"telefone2\" value=\"" . $cliente->get_telefone2() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">Rua</td>
				<td>
					<input type=\"text\" name=\"rua\" value=\"" . $cliente->get_endereco()->get_rua() . "\"  size=\"80\"/>
				</td>
			</tr>
			<tr>
				<td align=\"right\">Número</td>
				<td>
					<input type=\"text\" name=\"numero\" value=\"" . $cliente->get_endereco()->get_numero() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">Complemento</td>
				<td>
					<input type=\"text\" name=\"complemento\" value=\"" . $cliente->get_endereco()->get_complemento() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">Bairro</td>
				<td>
					<input type=\"text\" name=\"bairro\" value=\"" . $cliente->get_endereco()->get_bairro() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">Cidade</td>
				<td>
					<input type=\"text\" name=\"cidade\" value=\"" . $cliente->get_endereco()->get_cidade() . "\"  size=\"80\"/>
				</td>
			</tr>
			<tr>
				<td align=\"right\">Estado</td>
				<td>
					<input type=\"text\" name=\"estado\" value=\"" . $cliente->get_endereco()->get_estado() . "\" />
				</td>
			</tr>
			<tr>
				<td align=\"right\">CEP</td>
				<td>
					<input type=\"text\" name=\"cep\" value=\"" . $cliente->get_endereco()->get_cep() . "\" />
				</td>
			</tr>
			<tr>
				<td colspan=\"2\" align=\"center\">
					<button type=\"submit\">Salvar</button>
					<button type=\"button\" onclick=\"javascript: location.href='index.php';\">Cancelar</button>
				</td>
			</tr>
		</table>
	</form>
";

$design->imprimir_rodape();

?>